package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.TimestampUtil;
import util.enviaEmail;
import bean.OSBean;
import conexao.Conexao;
import dao.OSDao;

public class mediaOs extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public mediaOs() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Connection conn = null;
		try {
			conn = new Conexao().getConexao();
		} catch (SQLException e) {
			new enviaEmail().enviar(e);
			out.print("falhaConexao");
			return;
		}

		String tipoMedia = request.getParameter("tipoMedia");
		
		if (tipoMedia.equals("mediaTempoOs")) {

			try {
				Timestamp dataInicial = new TimestampUtil()
						.parseTimestampHoraInicial(request
								.getParameter("dataInicial"));

			Timestamp dataFinal = new TimestampUtil()
					.parseTimestampHoraFinal(request.getParameter("dataFinal"));
			
			OSBean os = new OSBean();
			os.setIdEmpresa((Integer) request.getSession().getAttribute("idempresa"));
			String media = new OSDao().getMediaTempoOs(conn, dataInicial, dataFinal, os);
			
			if(media.equals("nan")){
				out.println("Sem resultado.");
			}else{
				out.println(media);
			}
			

			} catch (ParseException e) {
				new enviaEmail().enviar(e);
				out.println("Data inválida.");
				return;
			} catch (Exception e) {
				try {
					conn.rollback();
				} catch (Exception ex) {
					new enviaEmail().enviar(ex);
					ex.printStackTrace();}
					new enviaEmail().enviar(e);
					throw new RuntimeException(e);
			} finally {
				try {
					conn.close();
				} catch (Exception ex) {
					new enviaEmail().enviar(ex);
				}
			}
		}
		if (tipoMedia.equals("mediaTempoOsDesigner")) {

			try {
				Timestamp dataInicial = new TimestampUtil()
						.parseTimestampHoraInicial(request
								.getParameter("dataInicial"));

			Timestamp dataFinal = new TimestampUtil()
					.parseTimestampHoraFinal(request.getParameter("dataFinal"));
			
			Integer id = Integer.parseInt(request.getParameter("id"));
			
			
			String media = new OSDao().getMediaTempoOsDesigner(conn, dataInicial, dataFinal, id);
			
			if(media.equals("nan")){
				out.println("Sem resultado com esses dados.");
			}else{
				out.println(media);
			}
			

			} catch (ParseException e) {
				new enviaEmail().enviar(e);
				out.println("Data inválida.");
				return;
			} catch (Exception e) {
				try {
					conn.rollback();
				} catch (Exception ex) {
					new enviaEmail().enviar(ex);
					ex.printStackTrace();}
					new enviaEmail().enviar(e);
					throw new RuntimeException(e);
			} finally {
				try {
					conn.close();
				} catch (Exception ex) {
					new enviaEmail().enviar(ex);
				}
			}
		}
		
		

	}

}
